<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css"  media="all">
    <script src="layui/layui.js" charset="utf-8"></script>

</head>
<body>

<div class="layui-input-block layui-form" id="modifyClass"
     lay-filter="updateClass" style="margin-top: 30px">
    <form class="layui-form layui-form-pane" action="">

        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text"  id="name" name="name" autocomplete="off" required lay-verify="required"
                           class="layui-input" placeholder="请输入优惠券名称">
                </div>

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" id="desc" name="desc" autocomplete="off"
                           class="layui-input" placeholder="请输入简单描述">
                </div>
                <div class="layui-input-inline">
                    <img class="layui-upload-img" id="img" width="150px" height="50px">
                    <p id="demoText"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <input type="text"  name="colletExpire" id="colletExpire" required lay-verify="required"
                       autocomplete="off" class="layui-input" placeholder="申领有效期">
            </div>
            <div class="layui-inline">
                <input type="text"  name="useExpire" id="useExpire" required lay-verify="required"
                       autocomplete="off" class="layui-input" placeholder="使用有效期">
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 390px">
            <label class="layui-form-label">使用规则</label>
            <textarea placeholder="请输入使用规则"  required lay-verify="required" name="role" id="role"
                      class="layui-textarea"></textarea>
        </div>
        <div class="layui-form-item">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="update">立即修改</button>
        </div>
        <input type="text" id="cpid" style="display: none">
    </form>
</div>

<script>


    layui.use('laydate', function() {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#colletExpire,#useExpire'
            ,type: 'datetime'
        });
    })
</script>

<!--  修改监听-->
<script>
    layui.use('form', function () {
        var $ = layui.jquery;
        var form = layui.form;
        //监听提交
        form.on('submit(update)', function (data) {

           //更新的是后拿值,这里还没有写完
            //
            //
            // "id":data.id,
            //     "name":data.name,
            //     "desc":data.desc,
            //     "img":data.img,
            //     "role":data.role,
            //     "colletExpire":data.colletExpire,
            //     "inUse":data.inUse,
            //     "useExpire":data.useExpire
            var jsonObject= {
                "id":$("#cpid").val(),
                "name":$("#name").val(),
                "desc":$("#desc").val(),
                "img":"",
                "role":$("#role").val(),
                "collectExpire":$("#colletExpire").val(),
                "useExpire":$("#useExpire").val()
            };
            $.ajax({
                url: "/cp"
                , type: "post"
                , headers: {"method": "upData"}
                , dataType: "json"
                , data: {"formData": JSON.stringify(jsonObject)}  //前端的所有字段
                , success: function (result) {   // result就是服务器响应之后返回的数据

                    if (result.code == 0) {
                        alert(result.msg);
                        // alert("添加成功")
                    } else {
                        // alert("添加失败")
                        alert(result.msg);
                    }

                }
                , error: function (e) {
                    console.log(e)
                }
            })
            return false;
        });

    });


</script>
</body>
</html>